﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BKO.UIControls
{
	public static class UIExtensions
	{
		public static void Bind(this ComboBox comboBox, IEnumerable<string> source,
			bool addBlank = false, bool clearFirst = true)
		{
			if (clearFirst) comboBox.Items.Clear();
			if (addBlank) comboBox.Items.Add(string.Empty);

			comboBox.Items.AddRange(source.ToArray());
		}

		public static void LoadIcons(this ImageList list, string pathPrefix, string path, string iconSize, string frm)
		{
			list.Images.Clear();

			list.ImageSize = iconSize == "32x32"
				? new Size(32, 32)
				: new Size(24, 24);

			string fullPath = Path.Combine(pathPrefix, path);

			try
			{
				for (int i = 1; i < 10; i++)
				{
					list.Images.Add(Image.FromFile(fullPath + "A" + i + ".png"));
				}

				for (int i = 1; i < 10; i++)
				{
					list.Images.Add(Image.FromFile(fullPath + "B" + i + ".png"));
				}

				for (int i = 1; i < 10; i++)
				{
					list.Images.Add(Image.FromFile(fullPath + "C" + i + ".png"));
				}

				for (int i = 1; i < 10; i++)
				{
					list.Images.Add(Image.FromFile(fullPath + "D" + i + ".png"));
				}

				for (int i = 1; i < 10; i++)
				{
					list.Images.Add(Image.FromFile(fullPath + "E" + i + ".png"));
				}

				for (int i = 1; i < 10; i++)
				{
					list.Images.Add(Image.FromFile(fullPath + "F" + i + ".png"));
				}

				if (frm != "DIR")
				{
					for (int i = 1; i < 10; i++)
					{
						list.Images.Add(Image.FromFile(fullPath + "G" + i + ".png"));
					}

					for (int i = 1; i < 6; i++)
					{
						list.Images.Add(Image.FromFile(fullPath + "H" + i + ".png"));
					}

					list.Images.Add(Image.FromFile(fullPath + "I1.png"));
				}

				string[] allowedExtensions = new[] { ".png" };
				string[] dirFiles = Directory.GetFiles(Path.Combine(pathPrefix, "img"));

				foreach (string dirFile in dirFiles)
				{
					foreach (string ext in allowedExtensions)
					{
						if (ext == Path.GetExtension(dirFile))
						{
							list.Images.Add(Image.FromFile(dirFile));
						}
					}
				}
			}
			catch (FileNotFoundException ex)
			{
				MessageBox.Show(string.Format("Не могу загрузить файл: {0}", ex.Message));
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
	}
}
